/*
$.ajaxSetup({
  cache: false
});
 */
 

$().ready( function() {
	
	getData();	
	//Shadowbox.init();
	//alert($(window).width());
	
	
	
});
var shadowtimes = 0 ;
var changekey = 0 ;
function getData() {
	
	for ( var x = 0; x < _key_array.length; x++) {
		if (_key_array[x] == "kart") {
			var _kart_reg = new RegExp('\\[' + 'kart' + '\\]', 'g');
			var _kart_match = $(document.body).text().match(_kart_reg);
			if (_kart_match) {
				var kart_word = "<div id='kart_table'></div>";
				replaceword('[' + _key_array[x] + ']', kart_word);

			}
		} else {
			callbody(_key_array[x]);
		}
	}
	
	var _list_reg = new RegExp('\\[' + 'kart_list' + '\\]', 'g');
	var _list_match = $(document.body).text().match(_list_reg);
	if (_list_match) {
		var list_word = "<div id='kart_list'></div>";
		replaceword('[kart_list]', list_word);

		chkkart_list();
	}
	
	if(changekey == 0){
		//Shadowbox.init();
	}
	//chkkart();
}

function callbody(_key) {
	var _results = [];
	var _reg = new RegExp('\\[' + _key + '=(\\w+)\\]', 'g');
	var _matched = $(document.body).text().match(_reg);

	if (_matched) {//判斷key值是否存在頁面

		for ( var _cnt = 0; _cnt < _matched.length; _cnt++) {
			_results.push(/\[\w+=(\w+)\]/.exec(_matched[_cnt])[1]);
		}
		shadowtimes++;
		changekey++;
		$.post("Mkart/data.php", {
			'key' : _key,
			'id[]' : _results
		}, function(data) {
			var text = document.body.innerHTML;
			for ( var i = 0; i < data.length; i++) {
				var chang_word = "";
				var _keyword = new RegExp('\\[' + data[i].word + '\\]', 'g');
				if (data[i].price) {
					chang_word = '$' + data[i].price;
				} else if (data[i].buttonword) {
					chang_word = data[i].buttonword;
				}
				//	  replaceword(_keyword,chang_word);
				text = text.replace(_keyword, chang_word);
				
			}
			document.body.innerHTML = text;
			
			shadowtimes--;
			if(shadowtimes == 0){
				//Shadowbox.init();
			}
		}, "json");	
		
		

	}//end check
}

function chkkart(_key) {
	
	var _kart_match = document.getElementById('kart_table');
	if (_key) {
		$.get("Mkart/kart_data.php?pid=" + _key, function(ta) {
			var chang_word = "";
			chang_word = ta;
			if (chang_word && _kart_match) {
				document.getElementById('kart_table').innerHTML = chang_word;
			}
		});
	} else {
		$.get("Mkart/kart_data.php", function(ta) {
			var chang_word = "";
			chang_word = ta;
			if (chang_word && _kart_match) {
				document.getElementById('kart_table').innerHTML = chang_word;
			}
		});
		
	}
}

function chkkart_list() {
	$.get("Mkart/kart_list.php", function(ta) {
		var chang_word = "";
		chang_word = ta;
		if (chang_word) {
			document.getElementById('kart_list').innerHTML = chang_word;
		}
	});
}

function change_count(_id, _num, _count) {
	$.get("Mkart/kart_list.php?pid=" + _id + "&count=" + _count + "&num="
			+ _num, function(ta) {
		var chang_word = "";
		chang_word = ta;
		if (chang_word) {
			document.getElementById('kart_list').innerHTML = chang_word;
		}
		chkkart();
	});
}

function cancel_prod(_num) {
	$.get("Mkart/kart_cancel.php?num=" + _num, function() {
		
			chkkart_list();
			chkkart();
		});

}

function replaceword(a, b) {
	var text = "";
	text = document.body.innerHTML;
	text = text.replace(a, b);
	document.body.innerHTML = text;
}
